/*
 *@本js功能：教师主页
 *@本js作者：杨锡君
 *@编写时间：2017年11月28日
 */
$(function(){
	var Alert1 = new Alert(); 
	Alert1.init();
	var confirm1 = new Confirm();
	var $classChose = $("#classChose");
	var $courseChose = $("#courseChose");
	var $state = $("#state");
	var $courseTable = $(".courseTable");
	var $nowWeek = $(".nowWeek");
	var $preWeek = $(".preWeek");
	var $nextWeek = $(".nextWeek");
	var courseArr = [];
	function tableModel(opt){
		var mon = opt[0];
		var monday = mon.split(" ")[1];
		var mondate = mon.split(" ")[0];
		var tue = opt[1];
		var tuesday = tue.split(" ")[1];
		var tuedate = tue.split(" ")[0];
		var wed = opt[2];
		var wednesday = wed.split(" ")[1];
		var weddate = wed.split(" ")[0];
		var thu = opt[3];
		var thursday = thu.split(" ")[1];
		var thudate = thu.split(" ")[0];
		var fri = opt[4];
		var friday = fri.split(" ")[1];
		var fridate = fri.split(" ")[0];
		var sat = opt[5];
		var saturday = sat.split(" ")[1];
		var satdate = sat.split(" ")[0];
		var sun = opt[6];
		var sunday = sun.split(" ")[1];
		var sundate = sun.split(" ")[0];
		return "<tr>"
				+"<th></th>"	
				+"<th>"
				+"<div>"+ monday +"</div>"
				+"<div class=\"nowTime\">"+ mondate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ tuesday +"</div>"
				+"<div>"+ tuedate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ wednesday +"</div>"
				+"<div>"+ weddate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ thursday +"</div>"
				+"<div>"+ thudate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ friday +"</div>"
				+"<div>"+ fridate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ saturday +"</div>"
				+"<div>"+ satdate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ sunday +"</div>"
				+"<div>"+ sundate +"</div>"
				+"</th>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">上&emsp;午</div>"
				+"<div  class=\"fl\">"
				+"<div>第一节</div>"
				+"<div>第二节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ tuedate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ weddate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ thudate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ fridate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ satdate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ sundate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">下&emsp;午</div>"
				+"<div  class=\"fl\">"
				+"<div>第三节</div>"
				+"<div>第四节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">晚&emsp;上</div>"
				+"<div  class=\"fl\">"
				+"<div>第五节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"; 
	}
	function classDataModel(opt){
		var departname = opt.departmentName;
		var professionname = opt.professionName;
		var classNum = opt.stuTotalNum;
		var classMark = opt.classMark;
		if(classNum == null || classNum == undefined){
			classNum = 0;
		}
		if(classMark == 0){
			classMark = "教师排课";
		}else if(classMark == 1){
			classMark = "学生选课";
		}else{
			classMark = "--";
		}
		if(opt.departmentName == undefined){
            departname = "--";
		}
        if(opt.professionName == undefined){
            professionname = "--";
        }
		return "<p title="+ departname +">所属院系："+ departname +"</p>"
				+"<p title="+ professionname +">专&emsp;&emsp;业："+ professionname +"</p>"
				+"<p>班级人数："+ classNum +"人</p>"
				+"<p>班级类型："+ classMark +"</p>";
	}
	var classNoDataModel = "<p>所属院系：--</p>"
        +"<p>专&emsp;&emsp;业：--</p>"
        +"<p>班级人数：0人</p>"
        +"<p>班级类型：--</p>";
	var n = 0;
	var teaIndex = {
		init:function(){
			this.bindEvent();
		},
		bindEvent:function(){
			var self = this;
			self.teaData();
			self.getClassList();
			self.getTableData(timeObject.changedate());
			self.weekTap();
			$(".searchBtn").off("click").click(function(){
				self.getAllData($classChose.val(),$courseChose.val());
			});
		},
		getClassList:function(){
			var self = this;
			$.ajax({
				type:"post",
				url:"teacherTeachClasses",
				async:true,
				success:function(msg){
					msg = JSON.parse(msg);
					if(msg.result == "success"){
						if(msg.classList.length!=0){
							$.map(msg.classList, function(opt) {
								$classChose.append("<option value="+ opt.classId +">"+ opt.className +"</option>");
							});
							self.getCourseList($classChose.children("option:first").val());
							$classChose.off("change").change(function(){
								self.getCourseList($classChose.val());
							});
						}else{
                            $(".infoBox").html(classNoDataModel);
						}
						
					}else{
						Alert1.content = "获取班级失败";
						Alert1.init();
						Alert1.show();
					}
				}
			});
		},
		getCourseList:function(id){
			var self = this;
			var jsonStr = JSON.stringify({
				"classId":Number(id)
			});
			$.ajax({
				type:"post",
				url:"getAllCourseListByClassId",
				async:true,
				data:{
					"jsonStr":jsonStr
				},
				success:function(msg){
                    msg = JSON.parse(msg);
					$courseChose.empty();
					$.map(msg.courseList, function(opt) {
						$courseChose.append("<option value="+ opt.courseId +">"+ opt.courseName +"</option>");
					});
					if(n == 0){
						self.getAllData(id,$courseChose.children("option:first").val());
						n++;
					}
					
				}
			});
		},
		getAllData:function(classid,courseid){
			var self = this;
			var jsonStr = JSON.stringify({
				"classId":Number(classid),
				"courseId":Number(courseid)
			});
			$.ajax({
				type:"post",
				url:"teacherMainCensus",
				async:true,
				data:{
					"jsonStr":jsonStr
				},
				success:function(msg){
                    msg = JSON.parse(msg);
					$(".infoBox").html(classDataModel(msg));
					var passPercent = msg.passPercent;
					if(passPercent == null || passPercent == undefined){
						passPercent = 0;
					}
					var submitTestPercent = msg.submitTestPercent;
					if(submitTestPercent == null || submitTestPercent == undefined){
						submitTestPercent = 0;
					}
					var submitHomeworkPercent = msg.submitHomeworkPercent;
					if(submitHomeworkPercent == null || submitHomeworkPercent == undefined){
						submitHomeworkPercent = 0;
					}
                    passPercent = Math.floor(passPercent*100);
                    submitTestPercent = Math.floor(submitTestPercent*100);
                    submitHomeworkPercent = Math.floor(submitHomeworkPercent*100);
//                     new Chart("pie1","#5ac0f0").ratePie(passPercent);
// //					new Chart("pie2","#2dc7c9").ratePie(65);
// 					new Chart("pie3","#f28e42").ratePie(submitTestPercent);
// 					new Chart("pie4","#b6a2df").ratePie(submitHomeworkPercent);
					$("#pie1").html(passPercent+"%");
                    $("#pie2").html(submitTestPercent+"%");
                    $("#pie3").html(submitHomeworkPercent+"%");
                    $(".c1").animate({
                        height: passPercent+"%"
                    },1000);
                    $(".c2").animate({
                        height: submitTestPercent+"%"
                    },1000);
                    $(".c3").animate({
                        height: submitHomeworkPercent+"%"
                    },1000);
				}
			});
		},
		teaData:function(){
//			获取教师统计
			$.ajax({
				type:"post",
				url:"teacherMainCensus",
				async:true,
				success:function(msg){
                    msg = JSON.parse(msg);
					var teachClassNum = msg.teachClassNum;
					if(teachClassNum == null || teachClassNum == undefined){
						teachClassNum = 0;
					}
					var teachStuNum = msg.teachStuNum;
					if(teachStuNum == null || teachStuNum == undefined){
						teachStuNum = 0;
					}
					var checkTestNum = msg.checkTestNum;
					if(checkTestNum == null || checkTestNum == undefined){
						checkTestNum = 0;
					}
					var checkHomeworkNum = msg.checkHomeworkNum;
					if(checkHomeworkNum == null || checkHomeworkNum == undefined){
						checkHomeworkNum = 0;
					}
					$(".oldClass").html(teachClassNum);
					$(".oldStu").html(teachStuNum);
					$(".oldLab").html(checkTestNum);
					$(".oldWork").html(checkHomeworkNum);
				}
			});
		},
		weekTap:function(){
			var self = this;
//			上下周本周切换
			$nowWeek.off("click").click(function(){
				
				self.getTableData(timeObject.changedate());
				
			});
			$preWeek.off("click").click(function(){
				var date = new Date($(".nowTime").html()).getTime()-7*24*60*60*1000;
				var time = timeObject.changedateYear(date);
				self.getTableData(time);
			});
			$nextWeek.off("click").click(function(){
				var date = new Date($(".nowTime").html()).getTime()+7*24*60*60*1000;
				var time = timeObject.changedateYear(date);
				self.getTableData(time);
			});
		},
		getTableData:function(date){
//			获取已有课程
			var self = this;
			var jsonStr = {
					"date":date
			};	
			$.ajax({
				type:"post",
				url:"getCourseTabAjax",
				async:true,
				data:{
					"jsonStr":JSON.stringify(jsonStr)
				},
				success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.dateList.length!=0){
                        $courseTable.html(tableModel(msg.dateList));
					}
					if(msg.courseTabList.length != 0){
						$.each(msg.courseTabList, function(opt) {
							var that = this;
							$.each($(".tableAddSection[data-date*='"+ this.classDate +"']"), function(key) {
								if($(this).attr("data-num") == that.classNum){
                                    $(this).html(that. courseName);
									if(that.selectMark == 1){
                                        $(this).attr({"data-courseid":that.courseId,"data-value":"2","style":"background:"+that.courseColer,"title":that.courseName + "   " + that.siteName+ "    " +that.startTime+"--"+that.endTime});
                                    }else{
                                        $(this).attr({"data-courseid":that.courseId,"data-value":"2","style":"background:"+that.courseColer+" url(pc/images/ico_optional.png) no-repeat right top","title":that.courseName + "   " + that.siteName+ "    " +that.startTime+"--"+that.endTime});
									}
                                }
							});
						});
					}
				}
			});
			
		}
	};
	teaIndex.init();
});
